package com.hqyj.lx.controller;


import com.hqyj.lx.pojo.Exampaper;
import com.hqyj.lx.pojo.Question;
import com.hqyj.lx.service.ExampaperService;
import com.hqyj.lx.service.QuestionService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

@Controller
public class QuestionController {

    @Autowired
    private QuestionService questionService;
    @Autowired
    private ExampaperService exampaperService;
    //处理查询全部试题的请求 使用jsp
    @RequestMapping("/queryAllQuestion")
    public String queryAll(ModelMap modelMap){
        List<Question> questions = questionService.queryAllQuestion();
        modelMap.put("questions",questions);
        return "questionList";
    }

    @RequestMapping(path="/insertOneQuestion",method = {RequestMethod.POST})
    public String insertOne(Question question){
        int i = questionService.insertOneQuestion(question);
        if(i==1){
            return "InsertSuccess";
        }
        return "questionError";
    }

    @RequestMapping(path = "/deleteOneQuestion")
    public String deleteOne(int quesId){
        int i = questionService.deleteOneQuestion(quesId);
        if(i==1){
            return "redirect:queryAllQuestion";
        }
        return "questionError";
    }

    @RequestMapping("/updateOneQuestion")
    public String updateOne(Question question){
        System.out.println(question);
        int i = questionService.updateOneQuestion(question);
        if(i==1){
            return "redirect:queryAllQuestion";
        }
        return "questionError";
    }
    //通过id查询数据
    @RequestMapping("/questionQueryById")
    public String questionQueryById(int quesId,ModelMap modelMap){
        Question question = questionService.questionQueryById(quesId);
        System.out.println(question);
        modelMap.put("question",question);
        return "questionUpdate";
    }

    //通过courseName课程名查询试题
    @RequestMapping("/questionQueryByCourseName")
    public String questionQueryByCourseName(String courseName,ModelMap modelMap){
        List<Question> questions = questionService.questionQueryByCourseName(courseName);
        modelMap.put("questions",questions);
        return "questionList";
    }
    //显示试题内容
    @RequestMapping("/questionQueryTest")
    public String questionQueryTest(String courseName, int eId, HttpSession session){
        //传递课程名进来用于查询question试题表数据
        //传递eId进来，用于查询exampaper试卷表数据
        System.out.println(courseName+eId);
        //单选题的List集合
        String quesTypeDanXuan="单选题";
        List<Question> questionDanXuans = questionService.questionQueryByQuesType(courseName,quesTypeDanXuan);
        //多选题的List集合
        String quesTypeDuoXuan="多选题";
        List<Question> questionDuoXuans = questionService.questionQueryByQuesType(courseName, quesTypeDuoXuan);
        //判断题的List集合
        String quesTypePanDuan="判断题";
        List<Question> questionPanDuans = questionService.questionQueryByQuesType(courseName, quesTypePanDuan);

        session.setAttribute("questionDanXuans",questionDanXuans);
        session.setAttribute("questionDuoXuans",questionDuoXuans);
        session.setAttribute("questionPanDuans",questionPanDuans);


        //通过Id查询试卷表
        Exampaper exampaperTest = exampaperService.queryById(eId);
        int validateTime = exampaperTest.getValidateTime();
        int testTime=validateTime*60;
        session.setAttribute("testTime",testTime);
        session.setAttribute("exampaperTest",exampaperTest);
        String  type = (String)session.getAttribute("type");
        if(type.equals("0")){
            return "studentTestList";
        }
        return "questionQueryTestList";
    }

    //处理试卷做完的数据
    @RequestMapping("/questionTest")
    public String questionTest(HttpSession session){


        String  type = (String)session.getAttribute("type");
        if(type.equals("0")){
            return "redirect:/queryMyScore";
        }
        return "redirect:/queryStudentScore";
    }

}
